package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcCsunHumidistatConfigMapper;
import com.csun.cmny.provider.device.csun.gateway.GatewayMessageSender;
import com.csun.cmny.provider.model.constant.DeviceConstant;
import com.csun.cmny.provider.model.constant.LifeConstant;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.model.dto.humidistat.HumidistatConfigAndThresholdDto;
import com.csun.cmny.provider.model.vo.humidistat.HumidistatConfigAndThresholdVO;
import com.csun.cmny.provider.model.vo.life.LifeTransducerConfigVO;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.util.PublicUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
@Transactional
public class DmcCsunHumidistatConfigServiceImpl extends BaseService<DmcCsunHumidistatConfig> implements DmcCsunHumidistatConfigService {
    @Resource
    private DmcCsunHumidistatConfigMapper dmcCsunHumidistatConfigMapper;
    @Resource
    private DmcDeviceService dmcDeviceService;
    @Resource
    private DmcCsunHumidistatLocationService dmcCsunHumidistatLocationService;
    @Resource
    private DmcLifeTransducerRoomConfigService dmcLifeTransducerRoomConfigService;
    @Resource
    private DmcLifeTransducerDefaultConfigService dmcLifeTransducerDefaultConfigService;
    @Resource
    private GatewayMessageSender gatewayMessageSender;
    @Resource
    private DmcCsunHumidistatThresholdService dmcCsunHumidistatThresholdService;

    /**
     * 根据设备Id获取温湿度计配置
     *
     * @param deviceId the  device id
     * @return the dmc csun humidistat config
     */
    @Override
    public DmcCsunHumidistatConfig queryConfigByDeviceId(Long deviceId) {

        DmcCsunHumidistatConfig result = null;

        DmcDevice dmcDevice = dmcDeviceService.selectByKey(deviceId);
        if (PublicUtil.isEmpty(dmcDevice)) {
            return null;
        }

        // 从配置中获取
//        result = dmcCsunHumidistatConfigMapper.selectByPrimaryKey(deviceId);
//        if (PublicUtil.isNotEmpty(result)) {
//            return result;
//        }

        // 根据绑定方式获取配置
        DmcCsunHumidistatLocation dmcCsunHumidistatLocation = dmcCsunHumidistatLocationService
                .selectByKey(deviceId);
        if (PublicUtil.isEmpty(dmcCsunHumidistatLocation)) {
            return null;    // 设备尚未绑定
        }

        switch (dmcCsunHumidistatLocation.getBindType()) {
            case DeviceConstant.DEVICE_BIND_TYPE_CUSTOM:
                dmcCsunHumidistatLocation.setRoomId(0);
                break;
            case DeviceConstant.DEVICE_BIND_TYPE_ROOM :

                // 从房间配置中获取
                DmcLifeTransducerRoomConfig dmcLifeTransducerRoomConfig =
                        dmcLifeTransducerRoomConfigService.selectByKey(dmcCsunHumidistatLocation.getRoomId());
                if (PublicUtil.isEmpty(dmcLifeTransducerRoomConfig)) {
                    // 从该机构的配置中获取
                    DmcLifeTransducerDefaultConfig dmcLifeTransducerDefaultConfig =
                            dmcLifeTransducerDefaultConfigService.selectByKey(dmcDevice.getGroupId());
                    if (PublicUtil.isEmpty(dmcLifeTransducerDefaultConfig)) {
                        return null;
                    } else {
                        result = new DmcCsunHumidistatConfig();
                        result.setDeviceId(deviceId);
                        result.setTemperatureMeasuringTimeInterval(dmcLifeTransducerDefaultConfig.getTemperatureMeasuringTimeInterval());
                    }
                } else {
                    result = new DmcCsunHumidistatConfig();
                    result.setDeviceId(deviceId);
                    result.setTemperatureMeasuringTimeInterval(dmcLifeTransducerRoomConfig.getTemperatureMeasuringTimeInterval());
                }

                break;
            case DeviceConstant.DEVICE_BIND_TYPE_LIFE :
                // 从房间配置中获取
                DmcLifeTransducerRoomConfig dmcLifeTransducerRoomConfigs =
                        dmcLifeTransducerRoomConfigService.selectByKey(dmcCsunHumidistatLocation.getRoomId());
                if (PublicUtil.isEmpty(dmcLifeTransducerRoomConfigs)) {
                    // 从该机构的配置中获取
                    DmcLifeTransducerDefaultConfig dmcLifeTransducerDefaultConfig =
                            dmcLifeTransducerDefaultConfigService.selectByKey(dmcDevice.getGroupId());
                    if (PublicUtil.isEmpty(dmcLifeTransducerDefaultConfig)) {
                        return null;
                    } else {
                        result = new DmcCsunHumidistatConfig();
                        result.setDeviceId(deviceId);
                        result.setTemperatureMeasuringTimeInterval(dmcLifeTransducerDefaultConfig.getTemperatureMeasuringTimeInterval());
                    }
                } else {
                    result = new DmcCsunHumidistatConfig();
                    result.setDeviceId(deviceId);
                    result.setTemperatureMeasuringTimeInterval(dmcLifeTransducerRoomConfigs.getTemperatureMeasuringTimeInterval());
                }

                saveOrUpdate(result);
                break;
        }

        return result;
    }

    /**
     * 根据房间Id和机构Id获取温度传感器配置
     *
     * @param roomId
     * @param groupId
     * @return
     */
    @Override
    public DmcCsunHumidistatConfig queryConfigByRoomIdAndGroupId(Integer roomId, Long groupId) {

        DmcLifeTransducerRoomConfig dmcLifeTransducerRoomConfig =
                dmcLifeTransducerRoomConfigService.selectByKey(roomId);

        DmcCsunHumidistatConfig dmcCsunHumidistatConfig = null;

        if (PublicUtil.isEmpty(dmcLifeTransducerRoomConfig)) {
            // 从该机构的配置中获取
            LifeTransducerConfigVO vo = dmcLifeTransducerDefaultConfigService.getDefaultConfigByGroupId(groupId);
//            if (PublicUtil.isEmpty(dmcLifeTransducerDefaultConfig)) {
//                return null;
//            } else {
                dmcCsunHumidistatConfig = new DmcCsunHumidistatConfig();
                dmcCsunHumidistatConfig.setTemperatureMeasuringTimeInterval(vo.getTemperatureMeasuringTimeInterval());
//            }
        } else {
            dmcCsunHumidistatConfig = new DmcCsunHumidistatConfig();
            dmcCsunHumidistatConfig.setTemperatureMeasuringTimeInterval(dmcLifeTransducerRoomConfig.getTemperatureMeasuringTimeInterval());
        }

        return dmcCsunHumidistatConfig;
    }

    /**
     * 保存或刷新
     *
     * @param dmcCsunHumidistatConfig
     * @return
     */
    @Override
    public Integer saveOrUpdate(DmcCsunHumidistatConfig dmcCsunHumidistatConfig) {

        return dmcCsunHumidistatConfigMapper.saveOrUpdate(dmcCsunHumidistatConfig);
    }

    @Override
    public Integer setCsunHumidistatConfigAndThreshold(HumidistatConfigAndThresholdDto dto, LoginAuthDto loginAuthDto) {

        List<Long> deviceIdList = dto.getDeviceIdList();
        int i = 0;

        for (Long deviceId : deviceIdList) {


                //1.下发时间指令
//            DmcCsunHumidistatConfig csunHumidistatConfig = selectByKey(deviceId);
                DmcCsunHumidistatConfig config = new DmcCsunHumidistatConfig();

                config.setDeviceId(deviceId);
                config.setTemperatureMeasuringTimeInterval(dto.getTemperatureMeasuringTimeInterval());
                config.setIsSend(LifeConstant.DEVICE_ORDER_NEED_SEND);
                config.setLastOperator(loginAuthDto.getUserName());
                config.setLastOperatorId(loginAuthDto.getUserId());

//            if(csunHumidistatConfig == null){
                config.setCreator(loginAuthDto.getUserName());
                config.setCreatorId(loginAuthDto.getUserId());
//                dmcCsunHumidistatConfigMapper.insertSelective(config);
//            }else{
                dmcCsunHumidistatConfigMapper.saveOrUpdate(config);
//            }

                /**
                 * @date    2020-01-07
                 * @author  chisj
                 * @desc    下发温湿度配置
                 */
                gatewayMessageSender.setHumidistatTimer(deviceId, dto.getTemperatureMeasuringTimeInterval());

                //2.设置阈值
                DmcCsunHumidistatThreshold csunHumidistatThreshold = dmcCsunHumidistatThresholdService.selectByKey(deviceId);
                DmcCsunHumidistatThreshold threshold = new DmcCsunHumidistatThreshold();

                threshold.setTemperatureMaximum(dto.getTemperatureMaximum());
                threshold.setTemperatureMinimum(dto.getTemperatureMinimum());
                threshold.setDeviceId(deviceId);
                threshold.setLastOperator(loginAuthDto.getUserName());
                threshold.setLastOperatorId(loginAuthDto.getUserId());

                if(csunHumidistatThreshold == null){
                    threshold.setCreator(loginAuthDto.getUserName());
                    threshold.setCreatorId(loginAuthDto.getUserId());
                    dmcCsunHumidistatThresholdService.save(threshold);
                }else{
                    dmcCsunHumidistatThresholdService.update(threshold);
                }

                i++;



        }
        return i;
    }

    @Override
    public HumidistatConfigAndThresholdVO getCsunHumidistatConfigAndThreshold(Long deviceId) {

        HumidistatConfigAndThresholdVO vo = new HumidistatConfigAndThresholdVO();
        DmcCsunHumidistatConfig dmcCsunHumidistatConfig = dmcCsunHumidistatConfigMapper.selectByPrimaryKey(deviceId);
        DmcCsunHumidistatThreshold dmcCsunHumidistatThreshold = dmcCsunHumidistatThresholdService.selectByKey(deviceId);
        if(dmcCsunHumidistatConfig != null){
            BeanUtils.copyProperties(dmcCsunHumidistatConfig,vo);
        }
        if(dmcCsunHumidistatThreshold != null){
            BeanUtils.copyProperties(dmcCsunHumidistatThreshold,vo);
        }
        return vo;
    }
}
